﻿namespace WMS6.Web.Common
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;

    public class LpnSplit : BasePage
    {
        protected HtmlInputButton btnCancel;
        protected Button btnGenerate;
        protected Button btnPrint;
        protected Button btnSplit;
        protected HtmlGenericControl divMsg;
        protected HtmlForm form1;
        protected GridView gvLpn;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnLpnIDs;
        protected HtmlHead Head1;
        protected Label Label2;
        protected Label lblCount;
        protected Label lblLotAttr01;
        protected Label lblLpnID;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblQty;
        protected Label lblSkuID;
        protected Label lblSplitQty;
        protected HtmlLink mycss;
        protected TextBox txtCount;
        protected TextBox txtLotAttr01;
        protected TextBox txtLpnID;
        protected TextBox txtOwnerID;
        protected TextBox txtQty;
        protected TextBox txtSkuID;
        protected TextBox txtSplitQty;

        public List<decimal> AllocationQty(int Count, decimal TotalQty, decimal Qty)
        {
            List<decimal> dqty = new List<decimal>();
            for (int i = Count; i > 0; i--)
            {
                if (Qty > 0M)
                {
                    if (TotalQty > Qty)
                    {
                        TotalQty -= Qty;
                        dqty.Add(Qty);
                    }
                    else
                    {
                        dqty.Add(TotalQty);
                        TotalQty = 0M;
                    }
                }
                else
                {
                    dqty.Add(0M);
                }
            }
            if ((TotalQty > 0M) && (Qty > 0M))
            {
                List<decimal> CS00002;
                int CS00003;
                (CS00002 = dqty)[CS00003 = Count - 1] = CS00002[CS00003] + TotalQty;
            }
            return dqty;
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            if (this.gvLpn.Rows.Count > 0)
            {
                List<LpnInfo> lpnList = this.ViewState["LpnInfoList"] as List<LpnInfo>;
                if (lpnList.Count > 0)
                {
                    decimal dqty = 0M;
                    for (int i = 0; this.gvLpn.Rows.Count > i; i++)
                    {
                        string strLpnID = ((Label) this.gvLpn.Rows[i].FindControl("lblLpn")).Text;
                        for (int n = 0; lpnList.Count > n; n++)
                        {
                            if (strLpnID == lpnList[n].LpnID)
                            {
                                if (!(string.IsNullOrEmpty(((TextBox) this.gvLpn.Rows[i].FindControl("txtQty")).Text) || (decimal.Parse(((TextBox) this.gvLpn.Rows[i].FindControl("txtQty")).Text) <= 0M)))
                                {
                                    lpnList[n].Qty = decimal.Parse(((TextBox) this.gvLpn.Rows[i].FindControl("txtQty")).Text);
                                    dqty += lpnList[n].Qty;
                                }
                                else
                                {
                                    this.lblMsg.Text = "行项数量为0,请分配数量!";
                                    return;
                                }
                            }
                        }
                    }
                    if (decimal.Parse(this.txtQty.Text) != dqty)
                    {
                        this.lblMsg.Text = "拆分数量不等于原始数量!";
                    }
                    else
                    {
                        InventoryInfo inventoryInfo = this.ViewState["InventoryInfo"] as InventoryInfo;
                        string strType = base.GetValueFromAppSettings("LPNGenerateType");
                        try
                        {
                            string temp = InventoryMgtServiceClientProxy.GetInstance().LpnSplit(lpnList, inventoryInfo, this.txtLpnID.Text, strType);
                            if (string.IsNullOrEmpty(temp))
                            {
                                this.lblMsg.Text = "拆分成功!";
                                this.btnGenerate.Enabled = false;
                                this.btnSplit.Enabled = false;
                                this.btnPrint.Visible = true;
                            }
                            else
                            {
                                this.lblMsg.Text = temp;
                            }
                        }
                        catch (Exception ex)
                        {
                            this.lblMsg.Text = "拆分异常：" + ex.Message;
                        }
                    }
                }
                else
                {
                    this.lblMsg.Text = "请先分解Lpn!";
                }
            }
            else
            {
                this.lblMsg.Text = "请先分解Lpn!";
            }
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            List<LpnInfo> lpnList = this.ViewState["LpnInfoList"] as List<LpnInfo>;
            if ((lpnList != null) && (lpnList.Count > 0))
            {
                string lpnIDs = string.Empty;
                for (int i = 0; i < lpnList.Count; i++)
                {
                    lpnIDs = lpnIDs + "'" + lpnList[i].LpnID + "',";
                }
                this.hdnLpnIDs.Value = lpnIDs.Substring(0, lpnIDs.Length - 1);
                this.Session["PrintLpnIDS"] = lpnIDs.Substring(0, lpnIDs.Length - 1);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>popPrinLpnLabel();</script>", false);
        }

        protected void btnSplit_Click(object sender, EventArgs e)
        {
            if (this.ViewState["InventoryInfo"] != null)
            {
                InventoryInfo oldlpnInfo = this.ViewState["InventoryInfo"] as InventoryInfo;
                if (oldlpnInfo != null)
                {
                    string strLpnType = string.Empty;
                    List<decimal> dQty = new List<decimal>();
                    string strLpn = string.Empty;
                    string NewLpn = string.Empty;
                    string lpnVersion = string.Empty;
                    if (oldlpnInfo.LpnID.Length == 0x11)
                    {
                        strLpn = oldlpnInfo.LpnID.Substring(oldlpnInfo.LpnID.Length - 4);
                        NewLpn = oldlpnInfo.LpnID.Substring(0, oldlpnInfo.LpnID.Length - 4);
                        lpnVersion = "N";
                    }
                    else
                    {
                        if (oldlpnInfo.LpnID.Substring(oldlpnInfo.LpnID.Length - 4, 3) == "000")
                        {
                            strLpn = oldlpnInfo.LpnID.Substring(oldlpnInfo.LpnID.Length - 3);
                            NewLpn = oldlpnInfo.LpnID.Substring(0, oldlpnInfo.LpnID.Length - 3);
                        }
                        else
                        {
                            strLpn = oldlpnInfo.LpnID.Substring(oldlpnInfo.LpnID.Length - 2);
                            NewLpn = oldlpnInfo.LpnID.Substring(0, oldlpnInfo.LpnID.Length - 2);
                        }
                        lpnVersion = "O";
                    }
                    string NewNum = string.Empty;
                    if (int.Parse(strLpn) > 0)
                    {
                        strLpnType = "B";
                        NewNum = InventoryMgtServiceClientProxy.GetInstance().GetMaxLpnByLpnID(NewLpn, oldlpnInfo.LpnID.Length.ToString());
                        NewNum = NewNum.Substring(NewNum.Length - 4);
                        if (lpnVersion == "O")
                        {
                            NewNum = "00" + NewNum.Substring(NewNum.Length - 2);
                        }
                    }
                    else
                    {
                        strLpnType = "A";
                    }
                    if (!string.IsNullOrEmpty(this.txtCount.Text.Trim()))
                    {
                        if (this.txtCount.Text.Trim() == "0")
                        {
                            this.lblMsg.Text = "请输入大于0的拆分个数!";
                            this.txtCount.Focus();
                        }
                        else
                        {
                            decimal iSplitQty = 0M;
                            if (!string.IsNullOrEmpty(this.txtSplitQty.Text))
                            {
                                iSplitQty = decimal.Parse(this.txtSplitQty.Text);
                            }
                            dQty = this.AllocationQty(int.Parse(this.txtCount.Text), decimal.Parse(this.txtQty.Text), iSplitQty);
                            List<LpnInfo> lpnList = new List<LpnInfo>();
                            int Count = int.Parse(this.txtCount.Text);
                            for (int i = 0; Count > i; i++)
                            {
                                int CS00001;
                                LpnInfo lpninfo = new LpnInfo();
                                if (strLpnType == "A")
                                {
                                    CS00001 = 1 + i;
                                    lpninfo.LpnID = NewLpn + this.ValueConvert(CS00001.ToString(), 4);
                                }
                                else
                                {
                                    CS00001 = (int.Parse(NewNum) + i) + 1;
                                    lpninfo.LpnID = NewLpn + this.ValueConvert(CS00001.ToString(), 4);
                                }
                                if (dQty.Count > 0)
                                {
                                    lpninfo.Qty = dQty[i];
                                }
                                else
                                {
                                    lpninfo.Qty = 0M;
                                }
                                lpninfo.PackID = "";
                                lpninfo.IsActive = "";
                                lpninfo.DocID = "";
                                lpninfo.DocType = "";
                                lpninfo.Ipskey = "";
                                lpninfo.PutawayHi = 0M;
                                lpninfo.PutawayTi = 0M;
                                lpninfo.Status = oldlpnInfo.Status;
                                lpninfo.CreatedBy = base.UserInfo.UserID;
                                lpninfo.UpdatedBy = lpninfo.CreatedBy;
                                lpnList.Add(lpninfo);
                            }
                            this.ViewState["LpnInfoList"] = lpnList;
                            base.BindData2GridView(this.gvLpn, lpnList);
                            this.btnGenerate.Visible = true;
                            this.btnCancel.Visible = true;
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = "请输入要拆分的个数!";
                        this.txtCount.Focus();
                    }
                }
            }
            else
            {
                this.lblMsg.Text = "未找到需要分解的LPN";
            }
        }

        protected void gvLpn_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                this.hdnActionID.Value = base.Request.QueryString["ActionID"];
                if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
                {
                    InventoryInfo inventoryInfo = InventoryMgtServiceClientProxy.GetInstance().GetInventoryInfoByLpnID(base.Request.QueryString["ID"]);
                    if (inventoryInfo != null)
                    {
                        this.txtLpnID.Text = inventoryInfo.LpnID;
                        this.txtQty.Text = base.GetPreferenceDigit(inventoryInfo.Qty);
                        this.txtSkuID.Text = inventoryInfo.SkuID;
                        this.txtOwnerID.Text = inventoryInfo.OwnerID;
                        this.txtLotAttr01.Text = inventoryInfo.LotAttr01;
                        this.txtLpnID.Text = inventoryInfo.LpnID;
                        this.txtLpnID.Text = inventoryInfo.LpnID;
                        if (inventoryInfo.IsLpnPick == "Y")
                        {
                            this.lblMsg.Text = "Lpn已经有拣货记录无法拆分！";
                            this.btnSplit.Enabled = false;
                        }
                        this.ViewState["InventoryInfo"] = inventoryInfo;
                    }
                    else
                    {
                        this.lblMsg.Text = "未找到匹配的数据源！";
                    }
                }
                else
                {
                    this.lblMsg.Text = "未找到需要分解的LPN";
                }
            }
        }

        public string ValueConvert(string value, int num)
        {
            string temp = value;
            if (value.Length < num)
            {
                for (int i = value.Length; num > i; i++)
                {
                    temp = "0" + temp;
                }
            }
            return temp;
        }
    }
}

